//
//  MYXForgotPasswordViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/4.
//  Copyright © 2017年 hbjApple. All rights reserved.
//忘记密码

#import "MYXForgotPasswordViewController.h"
#import "MYXForgotPasswordCell.h"
#import "CodeButton.h"

static NSString *const MYXForgotPasswordCellIdentifier = @"MYXForgotPasswordCell";

@interface MYXForgotPasswordViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,UITextFieldDelegate>{
    NSString *phoneStr;
    NSString *codeStr;
    NSString *passwordStr;
    NSString *SurepasswordStr;
}
@property (nonatomic, strong)UITableView * m_tabView ;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;

@end

@implementation MYXForgotPasswordViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self createUI];
}


#pragma mark - UI

- (void)createUI
{
    [self.view addSubview:self.m_tabView];
    [self.m_tabView registerNib:[UINib nibWithNibName:MYXForgotPasswordCellIdentifier bundle:nil] forCellReuseIdentifier:MYXForgotPasswordCellIdentifier];
    
    
}

#pragma mark -忘记密码

-(UITableView *)m_tabView
{
    if (!_m_tabView)
    {
        _m_tabView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _m_tabView.backgroundColor =  [UIColor whiteColor];
        _m_tabView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _m_tabView.showsVerticalScrollIndicator = NO;
        _m_tabView.dataSource = self;
        _m_tabView.delegate = self;
        WS(weakSelf)
    }
    return _m_tabView;
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     WS(weakSelf)
    __weak typeof(self) weakSe = self;
    MYXForgotPasswordCell *MYXForgotPasswordCe = (MYXForgotPasswordCell *)[tableView dequeueReusableCellWithIdentifier:MYXForgotPasswordCellIdentifier];
    MYXForgotPasswordCe.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row==1) {
        [MYXForgotPasswordCe.Forgotbutton setHidden:NO];
        MYXForgotPasswordCe.textfiled.placeholder = @"请输入验证码";
        MYXForgotPasswordCe.textfiled.keyboardType=UIKeyboardTypeNumberPad;
        MYXForgotPasswordCe.XVerificationB =^(CodeButton *btn){
        
            [weakSelf checkPhoneTextField:btn];
            
        };
      
        
    }else{
        [MYXForgotPasswordCe.Forgotbutton setHidden:YES];
        if (indexPath.row==0) {
            MYXForgotPasswordCe.textfiled.keyboardType=UIKeyboardTypeNumberPad;
            MYXForgotPasswordCe.textfiled.placeholder = @"请输入手机号";
            phoneStr =  MYXForgotPasswordCe.textfiled.text;
        }else if (indexPath.row==2){
             MYXForgotPasswordCe.textfiled.secureTextEntry = YES;
            if (self.type==2) {
                  MYXForgotPasswordCe.textfiled.placeholder = @"请输入支付密码";
                  MYXForgotPasswordCe.textfiled.keyboardType=UIKeyboardTypeNumberPad;

            }else{
                 MYXForgotPasswordCe.textfiled.placeholder = @"请输入新密码";
            }
            
             passwordStr =  MYXForgotPasswordCe.textfiled.text;
        }else if (indexPath.row==3){
            MYXForgotPasswordCe.textfiled.secureTextEntry = YES;
            if (self.type==2) {
               MYXForgotPasswordCe.textfiled.placeholder = @"再次确认支付密码";
               MYXForgotPasswordCe.textfiled.keyboardType=UIKeyboardTypeNumberPad;

            }else{
               MYXForgotPasswordCe.textfiled.placeholder = @"请再次确认密码";
            }
            
            SurepasswordStr =  MYXForgotPasswordCe.textfiled.text;
        }
       
    }
    MYXForgotPasswordCe.textfiled.delegate = self;
    MYXForgotPasswordCe.textfiled.tag = 1000+indexPath.row;
    return MYXForgotPasswordCe;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 51;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *fooerView =[[UIView alloc]init];
    fooerView.frame                         = CGRectMake(0, 0, WIDTH, 70);
    fooerView.backgroundColor =[UIColor whiteColor];
    
    UIButton *  _button = [[UIButton alloc] initWithFrame:CGRectMake(10, (fooerView.frame.size.height-(tab_h-10))/2, fooerView.frame.size.width-20, tab_h-10)];
    [_button setBackgroundColor:MYYheadColor];
    _button.clipsToBounds = YES;
    _button.layer.cornerRadius = 5;
    [_button setTitle:@"完成" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(entered) forControlEvents:UIControlEventTouchUpInside];
    _button.titleLabel.font =[UIFont systemFontOfSize:14];
    [fooerView addSubview:_button];
    return fooerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 70;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *HeaderView =[[UIView alloc]init];
    HeaderView.frame                         = CGRectMake(0, 0, WIDTH, 0.1);
    HeaderView.backgroundColor =[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    
    return HeaderView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.1;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


-(void)entered{
    
    if (self.type==2) {
        [self settIngPay];
    }else{
        [self forGotPassWord];
    }
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag-1000 ==0) {
       phoneStr =textField.text;
        
    }else if (textField.tag-1000 ==1){
       codeStr =textField.text;
    }else if (textField.tag-1000 ==2){
       passwordStr =textField.text;
    }else if (textField.tag-1000 ==3){
       SurepasswordStr =textField.text;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

/** 检查密码格式 */
- (BOOL)checkPasswordTextField {
    return passwordStr.length >= 6 && passwordStr.length <= 20;
}

- (void)checkPhoneTextField:(CodeButton *)btn {
    if (phoneStr.length<=0){
        [JXTHUDHelper showHUDText:@"请输入手机号" type:@"error" inView:nil];
        return;
    }
    NSString *firstStr = [phoneStr substringToIndex:1];
    if (phoneStr.length != 11||![firstStr isEqualToString:@"1"]||phoneStr.length<=0) {
        [JXTHUDHelper showHUDText:@"请输入正确的手机号" type:@"error" inView:nil];
         return;
    }
    
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self.MYXHttpManag identify:phoneStr  success:^(NSString *dataStr) {
        [JXTHUDHelper showHUDText:@"发送成功" type:@"success" inView:nil];
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:@"请稍后重试！" type:@"error" inView:nil];
    } netDown:^{
        
    }];
    
    [btn startCountdown];
}

/** 忘记密码 */

-(void)forGotPassWord{
     [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if (phoneStr.length<=0){
        [JXTHUDHelper showHUDText:@"请输入手机号" type:@"error" inView:nil];
        return ;
        
    }else if (phoneStr.length>0) {
        NSString *firstStr = [phoneStr substringToIndex:1];
        if (phoneStr.length != 11 || ![firstStr isEqualToString:@"1"]||phoneStr.length<=0) {
            [JXTHUDHelper showHUDText:@"请输入正确的手机号" type:@"error" inView:nil];
            return ;
            
        }
        
    }else if (codeStr.length<=0) {
        [JXTHUDHelper showHUDText:@"请输入验证码" type:@"error" inView:nil];
        return ;
    } // 判断密码格式是否正确，并请求验证码
    if (![self checkPasswordTextField]) {
        [JXTHUDHelper showHUDText:@"请输入密码" type:@"error" inView:nil];
        return ;
    }
    if (![passwordStr isEqualToString:SurepasswordStr]){
        [JXTHUDHelper showHUDText:@"和之前密码不一样～" type:@"error" inView:nil];
        return;
    }
    [self.MYXHttpManag phone:phoneStr code:codeStr password:passwordStr success:^(MYY_User *user) {
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
        [JXTHUDHelper showHUDText:@"网络出错" type:@"error" inView:nil];
    }];
}



-(void)settIngPay{
     [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if (phoneStr.length<=0){
        [JXTHUDHelper showHUDText:@"请输入手机号" type:@"error" inView:nil];
        return ;
        
    }else if (phoneStr.length>0) {
        NSString *firstStr = [phoneStr substringToIndex:1];
        if (phoneStr.length != 11 || ![firstStr isEqualToString:@"1"]||phoneStr.length<=0) {
            [JXTHUDHelper showHUDText:@"请输入正确的手机号" type:@"error" inView:nil];
            return ;
            
        }
        
    }else if (codeStr.length<=0) {
        [JXTHUDHelper showHUDText:@"请输入验证码" type:@"error" inView:nil];
        return ;
    } // 判断密码格式是否正确，并请求验证码
    if (passwordStr.length<=0) {
        [JXTHUDHelper showHUDText:@"请输入支付密码" type:@"error" inView:nil];
        return ;
    }if (passwordStr.length<6) {
        [JXTHUDHelper showHUDText:@"请设置不少于6位的密码" type:@"error" inView:nil];
        return ;
    }
    if (![passwordStr isEqualToString:SurepasswordStr]){
        [JXTHUDHelper showHUDText:@"和之前密码不一样～" type:@"error" inView:nil];
        return;
    }
    [self.MYXHttpManag setpaypwd:phoneStr code:codeStr paypassword:passwordStr success:^(NSString *dataStr) {
     
        //[JXTHUDHelper showHUDText:dataStr type:@"error" inView:nil];
        //为了防止键盘的闪屏，延迟0.25秒退出
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (self.selectBlock) {
                self.selectBlock(dataStr);
            }
            [self.navigationController popViewControllerAnimated:YES];
        });
       
        
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
         [JXTHUDHelper showHUDText:@"网络出错" type:@"error" inView:nil];
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
